Imports SD.LLBLGen.Pro.ORMSupportClasses
Imports Aricie.DNN.Services.Url.FriendlyUrl.DatabaseSpecific

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Data

    Public Class DnnAdapter
        Inherits DataAccessAdapter

        Private Shared _SchemaOverrides As SchemaNameOverwriteHashtable
        Private Shared _CatalogOverrides As CatalogNameOverwriteHashtable



        Public Shared ReadOnly Property SchemaOverrides() As SchemaNameOverwriteHashtable
            Get
                If _SchemaOverrides Is Nothing Then
                    _SchemaOverrides = New SchemaNameOverwriteHashtable(SchemaNameUsage.Default)
                    _SchemaOverrides.Add("dbo", NukeHelper.DnnDatabaseOwner.TrimEnd("."c))
                End If
                Return _SchemaOverrides
            End Get
        End Property




        Public Shared ReadOnly Property CatalogOverrides() As CatalogNameOverwriteHashtable
            Get
                If _CatalogOverrides Is Nothing Then
                    _CatalogOverrides = New CatalogNameOverwriteHashtable(CatalogNameUsage.Clear)
                End If
                Return _CatalogOverrides
            End Get
        End Property



        Sub New()
            MyBase.New(NukeHelper.DnnConnectionString, True, CatalogOverrides, SchemaOverrides)
        End Sub

        Protected Overrides Function GetFieldPersistenceInfo(ByVal field As SD.LLBLGen.Pro.ORMSupportClasses.IEntityField2) As SD.LLBLGen.Pro.ORMSupportClasses.IFieldPersistenceInfo

            Dim toReturn As IFieldPersistenceInfo = MyBase.GetFieldPersistenceInfo(field)
            If NukeHelper.DnnObjectQualifier <> "" Then
                toReturn = New FieldPersistenceInfo(toReturn.SourceSchemaName, NukeHelper.DnnObjectQualifier & toReturn.SourceObjectName, toReturn.SourceColumnName, _
                    toReturn.SourceColumnIsNullable, toReturn.SourceColumnDbType, toReturn.SourceColumnMaxLength, toReturn.SourceColumnScale, toReturn.SourceColumnPrecision, _
                    toReturn.IsIdentity, toReturn.IdentityValueSequenceName, toReturn.TypeConverterToUse, toReturn.ActualDotNetType)
            End If
            Return toReturn
        End Function

        Protected Overrides Function GetFieldPersistenceInfos(ByVal entity As SD.LLBLGen.Pro.ORMSupportClasses.IEntity2) As SD.LLBLGen.Pro.ORMSupportClasses.IFieldPersistenceInfo()
            Return Me.UpdateFieldPersistenceInfoArray(MyBase.GetFieldPersistenceInfos(entity))
        End Function

        Protected Overrides Function GetFieldPersistenceInfos(ByVal entityName As String) As SD.LLBLGen.Pro.ORMSupportClasses.IFieldPersistenceInfo()
            Return Me.UpdateFieldPersistenceInfoArray(MyBase.GetFieldPersistenceInfos(entityName))
        End Function

        Private Function UpdateFieldPersistenceInfoArray(ByVal tempResult As IFieldPersistenceInfo()) As IFieldPersistenceInfo()
            Dim toReturn() As IFieldPersistenceInfo
            If NukeHelper.DnnObjectQualifier <> "" Then
                toReturn = New IFieldPersistenceInfo(tempResult.Length - 1) {}
                For i As Integer = 0 To tempResult.Length - 1
                    toReturn(i) = New FieldPersistenceInfo(tempResult(i).SourceSchemaName, NukeHelper.DnnObjectQualifier & tempResult(i).SourceObjectName, tempResult(i).SourceColumnName, _
                                        tempResult(i).SourceColumnIsNullable, tempResult(i).SourceColumnDbType, tempResult(i).SourceColumnMaxLength, tempResult(i).SourceColumnScale, tempResult(i).SourceColumnPrecision, _
                                        tempResult(i).IsIdentity, tempResult(i).IdentityValueSequenceName, tempResult(i).TypeConverterToUse, tempResult(i).ActualDotNetType)
                Next
            Else
                toReturn = tempResult
            End If
            Return toReturn
        End Function

        Public Overrides Function CallRetrievalStoredProcedure(ByVal storedProcedureToCall As String, ByVal parameters() As System.Data.SqlClient.SqlParameter, ByVal dataSetToFill As System.Data.DataSet) As Boolean
            Return MyBase.CallRetrievalStoredProcedure(NukeHelper.DnnObjectQualifier & storedProcedureToCall, parameters, dataSetToFill)
        End Function

        Public Overrides Function CallRetrievalStoredProcedure(ByVal storedProcedureToCall As String, ByVal parameters() As System.Data.SqlClient.SqlParameter, ByVal tableToFill As System.Data.DataTable) As Boolean
            Return MyBase.CallRetrievalStoredProcedure(NukeHelper.DnnObjectQualifier & storedProcedureToCall, parameters, tableToFill)
        End Function

        Public Overrides Function CallActionStoredProcedure(ByVal storedProcedureToCall As String, ByVal parameters() As System.Data.SqlClient.SqlParameter) As Integer
            Return MyBase.CallActionStoredProcedure(NukeHelper.DnnObjectQualifier & storedProcedureToCall, parameters)
        End Function


    End Class




End Namespace
